package com.sixiaolong.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sixiaolong.common.CustomException;
import com.sixiaolong.common.R;
import com.sixiaolong.domain.*;
import com.sixiaolong.service.CategoryService;
import com.sixiaolong.service.SetMealDishService;
import com.sixiaolong.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetMealDishService setMealDishService;
//    @Autowired
//    private CacheManager cacheManager;

//    分页
    @GetMapping("/page")
    public R getPage(int page, int pageSize, String name){
//        赋值
        Page<Setmeal> pageInfo = new Page<>();
        Page<SetMealDto> pageInfo1 = new Page<>();
//        赋值除了records的基本数据   pageSize等
        BeanUtils.copyProperties(pageInfo,pageInfo1,"records");
//
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(name),Setmeal::getName,name);
        lqw.orderByDesc(Setmeal::getStatus).orderByDesc(Setmeal::getUpdateTime);
        Page<Setmeal> page1 = setmealService.page(pageInfo, lqw);
        List<SetMealDto> list = new ArrayList<>();
        page1.getRecords().forEach(e -> {
            SetMealDto setMealDto = new SetMealDto();
//            复制 record的基本数据
            BeanUtils.copyProperties(e,setMealDto);
            Category category = categoryService.getById(e.getCategoryId());
            if (category!=null){
                setMealDto.setCategoryName(category.getName());
                list.add(setMealDto);
            }
        });
        pageInfo1.setRecords(list);
        return R.success(pageInfo1);
    }

//    根据id查询套餐信息
    @GetMapping("/{id}")
    public R<SetMealDto> getByIdMeal(@PathVariable Long id){
        SetMealDto setMeal = setmealService.getByIdSetMeal(id);
        return R.success(setMeal);
    }

//    保存添加的套餐
    @PostMapping
    @CacheEvict(value = "setMealCache",allEntries = true)
    public R<String> saveMeal(@RequestBody SetMealDto setMealDto){
        setmealService.saveSetMeal(setMealDto);
        return R.success("添加成功");
    }

//    删除套餐
    @DeleteMapping
    @Transactional
    public R<String> deleteData(@RequestParam List<Long> ids){
        log.info("ids:"+ids);
//        查询所有套餐 看有没有是正在售卖当中  是的话就不能删除  select count(*) from .. where id in (..) and status=1
        LambdaQueryWrapper<Setmeal> lqw1 = new LambdaQueryWrapper<>();
        lqw1.in(Setmeal::getId,ids);
        lqw1.eq(Setmeal::getStatus,1);
        int count = setmealService.count(lqw1);
        if (count>0){
            throw new CustomException("有套餐正在售卖中，不能删除");
        }
//        ids.forEach(id -> {
//            setmealService.removeById(id);
//            LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
//            lqw.eq(SetmealDish::getSetmealId,id);
//            setMealDishService.remove(lqw);
//        });
        setmealService.removeByIds(ids);
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.in(SetmealDish::getSetmealId,ids);
        setMealDishService.remove(lqw);
        return R.success("删除成功");
    }

    //    批量停售起售
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setMealCache",allEntries = true)
    public R<String> updateStatus(@PathVariable int status, @RequestParam List<Long> ids){
//        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        log.info("1");

//        Long id = dish.getId();
//        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
//        lqw.in(dishService::getById,id);
        UpdateWrapper<Setmeal> uw = new UpdateWrapper<>();
        uw.in("id",ids);
        uw.set("status",status);
        setmealService.update(uw);
        return R.success("修改成功");
    }

    //    根据分类id查询菜品名称
    @GetMapping("/list")
    @Cacheable(value = "setMealCache",key = "#categoryId+'_'+1")
    public R<List<Setmeal>> getByIdDish(@RequestParam Long categoryId){
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
//        只查在售的
        lqw.eq(Setmeal::getStatus,1);
        lqw.eq(categoryId != null,Setmeal::getCategoryId,categoryId);
        lqw.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> setmeals = setmealService.list(lqw);
        return R.success(setmeals);
    }


}
